package com.com.typhoon.view;

import com.com.typhoon.entity.User;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import org.springframework.web.servlet.view.document.AbstractXlsView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @author shuya | dongfeng.zdf@alibaba-inc.com
 * @description
 * @date 2018/10/23 下午2:10
 * @description
 * @modified by
 * @since V2.0.0
 */
public class ExcelView extends AbstractXlsView {
    @Override
    protected void buildExcelDocument(Map<String, Object> model, Workbook workbook, HttpServletRequest httpServletRequest, HttpServletResponse response) throws Exception {
        // change the file name
        response.setHeader("Content-Disposition", "attachment; filename=\"my-xls-file.xls\"");

        @SuppressWarnings("unchecked")
        List<User> users = (List<User>) model.get("users");

        // create excel xls sheet
        Sheet sheet = workbook.createSheet("User Detail");
        sheet.setDefaultColumnWidth(30);

        // create style for header cells
        CellStyle style = workbook.createCellStyle();
        Font font = workbook.createFont();
        font.setFontName("Arial");
        style.setFillForegroundColor(HSSFColor.BLUE.index);
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        font.setBold(true);
        font.setColor(HSSFColor.WHITE.index);
        style.setFont(font);


        // create header row
        Row header = sheet.createRow(0);
        header.createCell(0).setCellValue("id");
        header.getCell(0).setCellStyle(style);
        header.createCell(1).setCellValue("name");
        header.getCell(1).setCellStyle(style);
        header.createCell(2).setCellValue("createTime");
        header.getCell(2).setCellStyle(style);
        header.createCell(3).setCellValue("sex");
        header.getCell(3).setCellStyle(style);
        header.createCell(4).setCellValue("age");
        header.getCell(4).setCellStyle(style);

        int rowCount = 1;

        for(User user : users){
            Row userRow =  sheet.createRow(rowCount++);
            userRow.createCell(0).setCellValue(user.getId());
            userRow.createCell(1).setCellValue(user.getName());
            userRow.createCell(2).setCellValue(user.getCreateTime());
            userRow.createCell(3).setCellValue(user.getSex());
            userRow.createCell(4).setCellValue((null == user.getAge() ?  "" : user.getAge().toString()));
        }
    }
}
